package pl.kosmowska.ewelina.client.app.activity;

import java.util.Date;
import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.ClientFactory;
import pl.kosmowska.ewelina.client.app.activity.places.SignInPlace;
import pl.kosmowska.ewelina.client.app.activity.places.StockEditPlace;
import pl.kosmowska.ewelina.client.app.activity.places.StockPlace;
import pl.kosmowska.ewelina.client.app.requestfactory.StockDataRequest;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.StockDataProxy;
import pl.kosmowska.ewelina.client.app.user.AuthEvent;
import pl.kosmowska.ewelina.client.app.user.AuthEvent.Auth;
import pl.kosmowska.ewelina.client.app.user.AuthEventHandler;
import pl.kosmowska.ewelina.client.views.StockEditView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class StockEditActivity extends AbstractActivity implements StockEditView.Presenter {

	private StockEditView view;

	private ClientFactory clientFactory;

	private StockDataProxy stockData;

	private EntityProxyId<StockDataProxy> stockDataId;

	public StockEditActivity(StockEditPlace place, ClientFactory clientFactory) {
		this.stockData = place.getStockData();
		this.stockDataId = place.getStockDataId();
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getStockEditView();
		view.setPresenter(this);
		initAuthorisationForView() ;
		getAllStockNames();
		if (stockData == null && stockDataId!=null) { 
			getStockData(stockDataId);
		} else if (stockData != null)
		{
			view.setData(stockData);
			startView();
		}
		containerWidget.setWidget(view.asWidget());

	}

	
	private void initAuthorisationForView() {
		if (view.isAlreadyInit() == false) {
			clientFactory.getEventBus().addHandler(AuthEvent.TYPE, new AuthEventHandler() {
				public void onAuthEvent(AuthEvent event) {
					Auth e = event.getAuthEvent();
					if (e == Auth.LOGGEDIN) {
						view.reInitView();
					} else if (e == Auth.LOGGEDOUT) {
						goTo(new SignInPlace());
					}
				}
			});
		}
		view.setAlreadyInit(true);
	}
	
	
	private void startView() {
		if (clientFactory.getIsLoggedIn() == null) {
			// wait for login event b/c hasn't happened yet
		} else if (clientFactory.getIsLoggedIn() == true) {
			view.reInitView();
		} else if (clientFactory.getIsLoggedIn() == false) { 
			goTo(new SignInPlace());
		}	
	}

	private void getStockData(EntityProxyId<StockDataProxy> stockDataId) {
		Request<StockDataProxy> request = clientFactory.getRequestFactory().getStockDataRequest().find(stockDataId);
		request.fire(new Receiver<StockDataProxy>() {
			public void onSuccess(StockDataProxy stockData) {
				setData(stockData);
				view.setData(stockData);
				startView();
			}

			public void onFailure(ServerFailure error) {
				view.showMessage(ViewMessages.ERROR_TIME_OUT);
			}
		});

	}
	private void setData(StockDataProxy stockData) {
		this.stockData = stockData;

	}


	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	@Override
	public void getAllStockNames() {
		clientFactory.getAppLoadingView().startProcessing();
		Request<List<String>> request = clientFactory.getRequestFactory().getStockDataRequest().getStockNames();
		request.fire(new Receiver<List<String>>() {
			public void onSuccess(List<String> stockList) {
				view.fillStockNameList(stockList);
				clientFactory.getAppLoadingView().stopProcessing();
			}
			public void onFailure(ServerFailure error) {
				view.showMessage(ViewMessages.ERROR_TIME_OUT);
				clientFactory.getAppLoadingView().stopProcessing();
			}

		});

	}

	@Override
	public void saveStockData(String name, float value, int stockNumber,
			Date startDate) {
		// get the requestContext
		StockDataRequest StockRequest = clientFactory.getRequestFactory().getStockDataRequest();

		// is it create or edit
		StockDataProxy data = null;
		if (stockData == null) { // insert|create
			data = StockRequest.create(StockDataProxy.class);

		} else { // update|edit
			data = StockRequest.edit(stockData);
		}

		// persist these
		data.setName(name);
		data.setStartValue(value);
		data.setStartDate(startDate);
		data.setStockNumber(stockNumber);


		StockRequest.persist(data).fire(new Receiver<StockDataProxy>() {
			public void onSuccess(StockDataProxy StockData) {
				goTo(new StockPlace());
			}
			public void onFailure(ServerFailure error) {
				view.showMessage(ViewMessages.ERROR_ON_PERSIST);
			}
		}); 

	}

	@Override
	public void goToStockPlace() {
		goTo(new StockPlace());
		
	}

}
